import java.util.Arrays;

public class WiggleSort {

    public static void main(String[] args) {
        WiggleSort wiggleSort = new WiggleSort();
        int[] nums = {2,1};
        wiggleSort.wiggleSort(nums);
    }

    public void wiggleSort1(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        if (n == 1 || n == 2) return;
        int[] temp = new int[n];
        int r = n / 2;
        int l = 0;
        int i = n - 1;
        if (n % 2 == 1) {
            temp[i--] = nums[l++];
            r++;
        }
        while (r < n) {
            temp[i--] = nums[r++];
            temp[i--] = nums[l++];
        }
        System.arraycopy(temp, 0, nums, 0, n);
    }

    public void wiggleSort(int[] nums) {
        int n = nums.length;
        int[] temp = new int[5001];
        for (int num : nums) {
            ++temp[num];
        }
        int j = 5000;
        for (int i = 1; i < n; i += 2) {
            while (temp[j] == 0) --j;
            nums[i] = j;
            --temp[j];
        }
        for (int i = 0; i < n; i += 2) {
            while (temp[j] == 0) --j;
            nums[i] = j;
            --temp[j];
        }
    }
}
